package com.gqj.meter.service.impl;

import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.meter.domain.MdIt;
import com.gqj.meter.mapper.MdItMapper;
import com.gqj.meter.service.IMdItService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;
import java.util.Map;

/**
 * 互感器Service业务层处理
 *
 * @author lxq
 * @date lxq-02-10
 */
@Service
public class MdItServiceImpl implements IMdItService {
    @Autowired
    private MdItMapper mdItMapper;
    private static final Logger log = LoggerFactory.getLogger(MdItServiceImpl.class);
    @Autowired
    protected Validator validator;

    /**
     * 查询互感器
     *
     * @param itId 互感器主键
     * @return 互感器
     */
    @Override
    public MdIt selectMdItByItId(Long itId) {
        return mdItMapper.selectMdItByItId(itId);
    }

    /**
     * 查询互感器列表
     *
     * @param mdIt 互感器
     * @return 互感器
     */
    @Override
    public List<MdIt> selectMdItList(MdIt mdIt) {
        return mdItMapper.selectMdItList(mdIt);
    }

    /**
     * 新增互感器
     *
     * @param mdIt 互感器
     * @return 结果
     */
    @Override
    public int insertMdIt(MdIt mdIt) {
        return mdItMapper.insertMdIt(mdIt);
    }

    /**
     * 修改互感器
     *
     * @param mdIt 互感器
     * @return 结果
     */
    @Override
    public int updateMdIt(MdIt mdIt) {
        mdIt.setMeterNo(mdIt.getRfidNo());
        return mdItMapper.updateMdIt(mdIt);
    }

    /**
     * 批量删除互感器
     *
     * @param itIds 需要删除的互感器主键
     * @return 结果
     */
    @Override
    public int deleteMdItByItIds(Long[] itIds) {
        return mdItMapper.deleteMdItByItIds(itIds);
    }

    /**
     * 删除互感器信息
     *
     * @param itId 互感器主键
     * @return 结果
     */
    @Override
    public int deleteMdItByItId(Long itId) {
        return mdItMapper.deleteMdItByItId(itId);
    }

    /**
     * 校验资产编号码是否唯一
     *
     * @param mdIt 互感器
     * @return 结果
     */
    public String checkMeterNoUnique(MdIt mdIt) {
        Long itId = StringUtils.isNull(mdIt.getItId()) ? -1L : mdIt.getItId();
        MdIt info = mdItMapper.checkMeterNoUnique(mdIt.getMeterNo());
        if (StringUtils.isNotNull(info) && info.getItId().longValue() != itId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验rfid编号/条形码是否唯一
     *
     * @param mdIt 互感器
     * @return 结果
     */
    public String checkRfidNoUnique(MdIt mdIt) {
        Long itId = StringUtils.isNull(mdIt.getItId()) ? -1L : mdIt.getItId();
        MdIt info = mdItMapper.checkRfidNoUnique(mdIt.getRfidNo());
        if (StringUtils.isNotNull(info) && info.getItId().longValue() != itId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 导入互感器数据
     *
     * @param itList          互感器列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    public String importId(List<MdIt> itList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(itList) || itList.size() == 0) {
            throw new ServiceException("导入互感器不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MdIt it : itList) {
            try {
                BeanValidators.validateWithException(validator, it);
                // 验证是否存在这个数据
                MdIt m = mdItMapper.selectMdItByRfidNo(it.getRfidNo());
                if (StringUtils.isNull(m)) {
                    it.setServiceStatus("00");
                    this.insertMdIt(it);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、互感器 " + it.getRfidNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    it.setItId(m.getItId());
                    this.updateMdIt(it);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、互感器 " + it.getRfidNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、互感器 " + it.getRfidNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、互感器 " + it.getRfidNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    //查询业务状态数量
    @Override
    public int[] getServiceStatusNum() {
        Map<String, Object> map = mdItMapper.getServiceStatusNum();

        int[] counts = new int[6];
        counts[0] = ((Long) map.get("count_00")).intValue();
        counts[1] = ((Long) map.get("count_01")).intValue();
        counts[2] = ((Long) map.get("count_02")).intValue();
        counts[3] = ((Long) map.get("count_03")).intValue();
        counts[4] = ((Long) map.get("count_04")).intValue();
        counts[5] = ((Long) map.get("count_05")).intValue();
        return counts;
    }

     /**
     * 通过rfidCode查询互感器信息
     *
     * @param rfidCode 互感器主键
     * @return 互感器
     */
     @Override
    public List<MdIt> selectMdItByRfidBath(String[] rfidCode){
         return mdItMapper.selectMdItByRfidBath(rfidCode);
     }
}
